package com.yanqu.road.server.gameplayer.module.activity.hanghai;

import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.player.DecorationInfo;
import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.pb.activity.HangHaiActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.DecorationMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.pb.HangHaiPb;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.util.List;

public class HangHaiModule extends GeneralModule {

    private long updateShipReqTime;

    public long getUpdateShipReqTime() {
        return updateShipReqTime;
    }

    public void setUpdateShipReqTime(long updateShipReqTime) {
        this.updateShipReqTime = updateShipReqTime;
    }

    public HangHaiModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        loginHangHai();
    }

    public void loginHangHai() {
        HangHaiConfig config = HangHaiMgr.getHangHaiConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            // 同步配置
            HangHaiActivityProto.SyncHangHaiConfigRespMsg.Builder configBuilder = HangHaiActivityProto.SyncHangHaiConfigRespMsg.newBuilder();
            configBuilder.setConfig(HangHaiPb.parseHangHaiConfigMsg(config, player.getLanguage()));
            player.sendPacket(ClientProtocol.U_HANGHAI_ACTIVITY_SYNC_CONFIG, configBuilder);

            // 同步数据
            HangHaiActivityProto.CrossHangHaiLoginReqMsg.Builder loginBuilder = HangHaiActivityProto.CrossHangHaiLoginReqMsg.newBuilder();
            loginBuilder.setActivityId(config.getActivityInfo().getActivityId());
            loginBuilder.setUnionUid(player.getUserInfo().getUnionUid());
            loginBuilder.setPlayer(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
            loginBuilder.setInitMoney(calInitMoney(config));
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_HANGHAI_ACTIVITY_LOGIN, loginBuilder, config.getActivityId());
        }
    }

    public long calInitMoney(HangHaiConfig config) {
        // 基础资金
        int base = config.getBaseMoney();

        // 称号加成
        BigDecimal titleAdd = BigDecimal.valueOf(player.getTitleId()).multiply(BigDecimal.valueOf(config.getTitleMoneyAddition()));

        // 称号加成
        BigDecimal decoAdd = BigDecimal.ZERO;
        long nowTime = System.currentTimeMillis() / 1000;
        for (UserDecoration decoration : player.getModule(DecorationModule.class).getUserDecorationMap().values()) {
            if (decoration.getEndTime() == 0 || decoration.getEndTime() > nowTime) {
                DecorationInfo decorationInfo = DecorationMgr.getDecorationInfo(decoration.getDecorationId());
                if (decorationInfo == null) {
                    log.info("HangHaiMgr calInitMoney error! DecorationInfo no found! activityId={}, decorationId={}, userId={}", config.getActivityId(), decoration.getDecorationId(), player.getUserId());
                    continue;
                }

                if (decorationInfo.getType() == eDecorationType.HonoraryTitle.getValue()) {
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(decoration.getDecorationId());
                    if (goodsInfo == null) {
                        log.info("HangHaiMgr calInitMoney error! GoodsInfo no found! activityId={}, decorationId={}, userId={}", config.getActivityId(), decoration.getDecorationId(), player.getUserId());
                        continue;
                    }

                    List<Integer> list = StringUtils.stringToIntegerList(goodsInfo.getExtendParam(), "\\|");
                    if (list.size() < 7) {
                        log.info("HangHaiMgr calInitMoney error! GoodsInfo paramList size error! activityId={}, decorationId={}, userId={}", config.getActivityId(), decoration.getDecorationId(), player.getUserId());
                        continue;
                    }

                    BigDecimal tmpAdd = BigDecimal.valueOf(list.get(6));
                    if (tmpAdd.compareTo(decoAdd) > 0) {
                        decoAdd = tmpAdd;
                    }
                }
            }
        }

        // 汇总资金
        return BigDecimal.valueOf(base).add(titleAdd).add(decoAdd).longValue();
    }

}
